package com.gx.smart.building;

import com.casic.gx.grpc.common.ComQuery;
import com.casic.gx.grpc.common.ComResp;
import com.gx.smart.lib.http.api.asynctask.CallBack;
import com.gx.smart.lib.http.api.asynctask.GrpcAsyncTask;
import com.gx.smart.lib.http.api.config.ApiConfig;
import com.gx.smart.lib_http.BuildConfig;
import com.huishi.gx.ib.grpc.lib.message.messagerecordapi.MessageRecordApiGrpc;
import com.huishi.gx.ib.grpc.lib.message.messagerecordapi.MessageRecordDto;
import com.huishi.gx.ib.grpc.lib.message.messagerecordapi.MessageRecordQueryResponse;
import com.huishi.gx.ib.grpc.lib.message.messagerecordapi.MessageRecordReadReq;
import com.orhanobut.logger.Logger;

import java.util.concurrent.TimeUnit;

import io.grpc.ManagedChannel;

/**
 * @author: xiaosy
 * 时间: 2022/4/7
 * @Description: java类作用描述
 * @Version: 1.0
 **/
public class MessageRecordApiService {
    private String host = BuildConfig.JOYLIFE_GRPC_UAA_SERIVER_URL;
    private String port = "80";

    private static class SingleTonHolder {
        private static final MessageRecordApiService INSTANCE = new MessageRecordApiService();
    }

    public static MessageRecordApiService getInstance() {
        return SingleTonHolder.INSTANCE;
    }

    private MessageRecordApiService() {

    }

    /**
     * 获取AuthStub  设置超时时间  8秒
     *
     * @param channel
     * @return
     */
    private MessageRecordApiGrpc.MessageRecordApiBlockingStub getStub(ManagedChannel channel) {
        return MessageRecordApiGrpc.newBlockingStub(channel)
                .withDeadlineAfter(ApiConfig.TIMEOUT_NETWORK, TimeUnit.SECONDS);

    }

    /**
     * 查询
     *
     * @param callBack
     * @return
     */
    public GrpcAsyncTask<String, Void, MessageRecordQueryResponse> messageRecordQuery(
            ComQuery comQuery,
            CallBack<MessageRecordQueryResponse> callBack) {
        return new GrpcAsyncTask<String, Void, MessageRecordQueryResponse>(callBack) {
            @Override
            protected MessageRecordQueryResponse doRequestData(ManagedChannel channel) {
                MessageRecordDto message = MessageRecordDto.newBuilder()
                        .setComQuery(comQuery)
                        .setTenantNo(1)
                        .build();
                Logger.d(message);
                MessageRecordQueryResponse response = null;
                try {
                    response = getStub(channel).messageRecordQuery(message);
                    Logger.d(response);
                } catch (Exception e) {
                    Logger.e(e, "messageRecordQuery");
                }
                return response;
            }
        }.setHost(host).setPort(port).doExecute();
    }


    /**
     * 设置已读未读
     * @param callBack
     * @return
     */
    public GrpcAsyncTask<String, Void, ComResp> messageRecordRead(
            String messageId,
            CallBack<ComResp> callBack) {
        return new GrpcAsyncTask<String, Void, ComResp>(callBack) {
            @Override
            protected ComResp doRequestData(ManagedChannel channel) {
                MessageRecordReadReq message = MessageRecordReadReq.newBuilder()
                        .setMessageId(messageId)
                        .setIsRead(1)//是否已读，1是 2否
                        .build();
                Logger.d(message);
                ComResp response = null;
                try {
                    response = getStub(channel).messageRecordRead(message);
                    Logger.d(response);
                } catch (Exception e) {
                    Logger.e(e, "messageRecordRead");
                }
                return response;
            }
        }.setHost(host).setPort(port).doExecute();
    }
}
